/**
 * @class js.underas.controller.ListViewPag
 * @param {String} p_title titulo do list view
 * @extends js.underas.core.Component
 */
js.underas.controller.ListViewAdv = new Class({
/**@lends js.underas.controller.ListView#*/
	"Extends":js.underas.core.Component
	,"_item_render":"DefaultItemRender"
	,"dataProvider":null
	,"_ind":0
	,"_pag":1
	,"_max_regs":20
	,"_min_scroll":800
	//,"_size_scroll":800
	,"_first":true
	,"_loading":false
	,"_itFilter":null
	,"_itOrderBy":null
	,"itemChange":null
	,"pageRequest":function(p_obj){}
	,"_varmod":null
	,"_islistview":true
	,"_rowhtml":'<div><div class="col-sm-12 col-sx-12"></div></div>'
	,"initialize":function(p_title){
		this.parent("div",'<div class="headgridview col-xs-12 col-sm-12 col-md-12"></div><div style="height:400px;overflow:auto;" class="tilecellgrid col-xs-12 col-sm-12 col-md-12"></div>');
		this._islistview = true;
		this._first=true;
		this._pag = 1;
		this._loading = false;
		this.dataProvider = new js.underas.util.ArrayList([]);	
		this.getEle().addClass("boxTileLine ListViewAdv");
		
		//this.getEle().appendHTML('<div id="lvp_'+this.uid+'_2" style="height:70px" class="col-xs-12"></div>');
		//this.getEle(".tilecellgrid").grab(new Element("div",{"class":"col-xs-12 last_elem_orfes","id":"lvp_"+this.uid+"_2","styles":{"height":"150px"}}),'bottom');
	
		
		this._itFilter = new js.underas.controller.InputText("");
		this._itFilter.setSize(7);
		this._itFilter.setLabel("filtrar");
		this._itFilter.setIcon("search");
		//this._itFilter.setIcon("refresh",2);
		//this._itFilter.addEvent("keyup",this.setFilter.bind(this));		
		this._itFilter.addEvent('keydown',this.setFilter.bind(this));
		
		this._itOrderBy = new js.underas.controller.Select("ordem");
		this._itOrderBy.setSize(5);
		this._itOrderBy.setLabel("ordem");
		this._itOrderBy.setIcon("chevron-down",2);		
		this._itOrderBy.setValueField("idColumn");
		this._itOrderBy.setLabelField("column");
		this._itOrderBy.addEvent("change",this.setOrder.bind(this));
		this._itOrderBy.getInput().set("data-orderfield","asc");
		this._itOrderBy.getEle(".addon2").addEvent("click",this.setOrderField.bind(this));		
		this.getEle().grab(this._itOrderBy.getEle(),"top");
		this.getEle().grab(this._itFilter.getEle(),"top");	
		//this.append(this._itSearch);
		//this.itemChange = function(p_item){};		
		this.getEle(".tilecellgrid").set("id","lvp_"+this.uid).addEvent('click:relay(.tilecell)',this.changeSelectedItem.bind(this));	
	}
	,"_start":function(){
		if(this._first){
			this._first=false;
			var elem = this.getEle(".tilecellgrid");	
			this._min_scroll = elem.getScrollSize().y - elem.getSize().y;
			elem.addEvent("scroll",
				function(evt){
					//console.log("y:"+elem.getScroll().y+" min:"+this._min_scroll);	
					if(elem.getScroll().y > this._min_scroll-40){
						//elem.scrollTo(0,0);
						//this._pag++;
						if(!this._loading){	
							this._pag++;
							this._loading=true;					
							this.pageRequest(this.getPaginationParam());
							//console.log("#y:"+elem.getScroll().y+" min:"+this._min_scroll);							
							//$("lvp_"+this.uid+"_2").addClass("any_normal_load any_request");							
						}
					}else if(elem.getScroll().y<5&&this._pag>1){
						//atualizar page 
						
						if(!this._loading){	
							this._pag--;
							this._loading=true;					
							this.pageRequest(this.getPaginationParam());
							//console.log("#y:"+elem.getScroll().y+" min:"+this._min_scroll);							
							//$("lvp_"+this.uid+"_2").addClass("any_normal_load any_request");							
						}
						
					}
				}.bind(this)
			);
		}
	}
	,"getPaginationParam":function(){
		var obj_r = {};						
		obj_r["pag"] = this._pag;
		obj_r["max"] =  this._max_regs;
		obj_r["orderBy"] = this._itOrderBy.getValue();
		obj_r["order"] = this._itOrderBy.getInput().get("data-orderfield");
		obj_r["filterBy"] =  this._itOrderBy.getValue();
		obj_r["filterValue"] =this._itFilter.getValue();
		return obj_r;		
	}
	,"setListHeight":function(p_height){
		var elem = this.getEle(".tilecellgrid");
		elem.setStyle("height",p_height);		
		this._min_scroll = elem.getScrollSize().y - elem.getSize().y;
		//this._size_scroll = 
		//console.log("posicao:"+posi.top);
		return this;
	}
	/**
	* alterar o nome da variavel do modulo.
	* @memberOf js.underas.controller.ListView#
	* @param {String} p_varmod
	*/	
	,"setVarModule":function(p_varmod){
		this.getEle().set("data-varmod",p_varmod);
		this._varmod=p_varmod;
	}
	,"getVarModule":function(){
		return this._varmod;
	}
	,"getModule":function(){		
		return window[this.getVarModule()];
	}
	,"setItemRender":function(p_itemrender){
		this._item_render = p_itemrender;
	}
	/**
	Returns the sum of all numbers passed to the function.
	@param {...string} num A positive or negative number
	*/
	,"getItemRender":function(){
		return this._item_render;
	}
	,"setDataProvider":function(p_dta){		
		if(p_dta){
			this.dataProvider.addAll(p_dta);			
			if(this.dataProvider.size() > 0){	
				if(this._first){
					this._first=false;
					var tmpColumns = this.getModule().getColumns();
					var dtaOrder = [];
					tmpColumns.each(function(cfield,index){
						var tcolumn = cfield["column"];
						if(tcolumn!="_id"){
							var indN = dtaOrder.length;
							//tcolumn.replace(/([a-z]{2})([A-Z])/
							dtaOrder[indN] = {"idColumn":tcolumn,"column":tcolumn};	
						};		
					});	
					this._itOrderBy.setDataProvider(dtaOrder);	
				};	
			};					
		}else{
			this.dataProvider.removeAll();
		}
		return this.refresh();
	}
	,"addDataProvider":function(p_dta){		
		if(p_dta){
			this.dataProvider.addAllArray(p_dta);					
		}		
		return this.refresh();
	}
	,"getDataProvider":function(){
		return this.dataProvider;
	}
	,"setHeight":function(p_height){
		this.getEle().setStyles({"height":p_height+"px"});
	}
	,"setRow":function(p_html){		
		this._rowhtml = p_html;
	}
	,"addRow":function(p_pg,p_html){		
		var tmpRow = null;
		if(p_html){
			tmpRow = Elements.from(p_html);
		}else{
			tmpRow = Elements.from(this._rowhtml); // new Element('div',{'class':'row_cells','html':this._rowhtml});	
		}
		tmpRow.set("data-citens",0).addClass('row_cells col pg_u'+p_pg);
		tmpRow.inject(this.getEle(".tilecellgrid"),"top");
		//this.getEle(".tilecellgrid").grab(tmpRow);
	}
	,"setHeadGrid":function(p_html){	
		this.getEle(".headgridview").set("html",p_html);	
	}
	,"getRow":function(p_index){
		return this.getEle(".tilecellgrid").getLast(".row_cells");
	}
	,"clear":function(){
		this.getEle(".tilecellgrid").set("html","");
	}
	,"refresh":function(){
		var dpt = this.dataProvider.size();
		this.clear();
		if(dpt > 0){				
			var qt_pg = 1;
			var qt_row = 0;
			var qt_cells = 0;
			varpgvisible = "block";	
			var tmpItemRender =  new window[this.getItemRender()](this.dataProvider.get(0));						
			var max_cells = tmpItemRender.getMaxCells();
			var max_rows = tmpItemRender.getMaxRows();
			tmpItemRender = null;		
			this.addRow(qt_pg);
			this.getRow().setStyle("display",varpgvisible);			
			for(var z=0;z<dpt;z++){			
				if(qt_cells >= max_cells){
					qt_cells=0;
					qt_row++;					
					this.addRow(qt_pg);
					this.getRow().setStyle("display",varpgvisible);
				}				
				qt_cells++;
				this.getRow().set("data-citens",qt_cells);				
				var item  =  this.dataProvider.get(z);
				item["_ind"]=z;
				var itemRender = {};
				itemRender = new window[this.getItemRender()](item);
				itemRender.getEle().set("data-ind",z).addClass("tilecell list-group-item "+this.getItemRender());
				this.getRow().grab(itemRender.getEle(),'bottom');								
			} 
			this.getEle(".tilecellgrid").grab(new Element("div",{"class":"col-xs-12 last_elem_orfes","styles":{"height":"150px"}}),'bottom');
		}
		this._start();
		return this;
	}
	,"setFilter":function(evt){
		if(evt.key == "enter"){	
			//console.log("e:"+evt.key);
			this._pag = 1;
			this._loading = false;
			this.pageRequest(this.getPaginationParam());		
		}
	}	
	,"setOrderField":function(evt){
		var oldOrder = this._itOrderBy.getInput().get("data-orderfield");
		var icontmp = "chevron-down";
		if(oldOrder=="asc"){
			oldOrder="desc";
			icontmp = "chevron-up";
			//this.orderDesc(this._itOrderBy.getValue());
		}else{
			oldOrder="asc";
			icontmp = "chevron-down";
			//this.orderAsc(this._itOrderBy.getValue());
		}
		this._itOrderBy.getInput().set("data-orderfield",oldOrder);
		this._itOrderBy.setIcon(icontmp,2);
		this._pag = 1;
		this._loading = false;
		this.pageRequest(this.getPaginationParam());
		
	}
	,"setOrder":function(evt){
		//this.orderAsc(this._itOrderBy.getValue());	
		this._pag = 1;
		this._loading = false;
		this.pageRequest(this.getPaginationParam());
	}
	,"orderDesc":function(p_campo){
		//this.dataProvider.orderDesc(p_campo);
		//this.refreshGridDate();
	}
	,"orderAsc":function(p_campo){
		//this.dataProvider.orderAsc(p_campo);
		//this.refreshGridDate();	
	}
	,"getSelectedIndex":function(){
        return this._ind;
	}
	,"getSelectedItem":function(){
        if(this.dataProvider.size() < this._ind){
            this._ind = 0;
        }
        return this.dataProvider.get(this._ind);
	}
	,"setSelectedItem":function(p_item){
		this.dataProvider.set(p_item,this._ind);
	}
	,"updateItem":function(p_item){
		this.setSelectedItem(p_item);
		var itemRender = {};
		itemRender = new window[this.getItemRender()](p_item);
		itemRender.getEle().set("data-ind",this.getSelectedIndex()).addClass("tilecell list-group-item "+this.getItemRender());
		this.getEle().getChildren(".tilecellgrid .selectedLine").grab(itemRender.getEle(),'before');
		this.getEle().getChildren(".tilecellgrid .selectedLine").destroy();
		itemRender.getEle().addClass("selectedLine");
	}
	,"insertItem":function(p_item,p_where){
		var lwhere = p_where||'top';
		//this.setSelectedItem(p_item);
		var nind = this.dataProvider.size();		
		this.dataProvider.add(p_item);
		this._ind = nind;
		var itemRender = {};
		p_item["_ind"]=nind;
		itemRender = new window[this.getItemRender()](p_item);
		//var uidItem = itemRender.getEle().get("id");
		itemRender.getEle().set("data-ind",nind).addClass("tilecell selectedLine list-group-item "+this.getItemRender());		
		var tcitens = 1;
		if(nind==0){
			this.addRow(1);
			this.getRow().setStyle("display","block");
		}else{
			var maxCells = itemRender.getMaxCells();
			var tcitens = parseInt(this.getRow().get("data-citens"));
			if(tcitens==maxCells){
				this.addRow(1);
				this.getRow().setStyle("display","block");
				tcitens=0;
			}
			tcitens++;
		}
		this.getRow().set("data-citens",tcitens).grab(itemRender.getEle(),lwhere);
		this.changeSelectedItem(null,itemRender.getEle());
	}
	,"removeSelectedItem":function(){
		//this.setSelectedItem();
		//nao remover do array para evitar null point
		this.getEle().getChildren(".tilecellgrid .selectedLine").destroy();
	}
	,"removeItem":function(p_item){
		this.removeSelectedItem();
	}
	,"changeSelectedItem":function(evt,tgt){		
		this.getEle(".tilecellgrid").getChildren(".row_cells .selectedLine").each(function(item){
			item.removeClass("selectedLine").removeClass("active");
		});		
		tgt.addClass("selectedLine active");		
		this["_ind"] = parseInt(tgt.get("data-ind"));		
		if(this.itemChange&&this.getSelectedItem()){
			this.itemChange(this.getSelectedItem());			
		};
		if(this.getSelectedItem()){
			this.getModule().onChangeItem(this.getSelectedItem());
		};
	}	
	,"setSelectedIndex":function(p_index){
		 this._ind = p_index;
	}
	,"changeToIndex":function(p_index){	
		if(this.getDataProvider().size()>p_index){
			this.setSelectedIndex(p_index);
			this.getEle(".tilecellgrid").getChildren(".row_cells .selectedLine").each(function(item){
				item.removeClass("selectedLine").removeClass("active");
			});	
			if(this.getSelectedItem()){
				this.getEle(".tilecellgrid").getChildren(".row_cells .tilecell")[p_index].addClass("selectedLine active");
			};
			if(this.itemChange&&this.getSelectedItem()){
				this.getModule().onChangeItem(this.getSelectedItem());
				this.itemChange(this.getSelectedItem());			
			};
		}else{
			this.getModule().clearFormItem();
			if(this.itemChange){
				this.itemChange();
			};
		};		       
	}
});
